package com.sdry.work.controller;

import com.sdry.common.BaseController;
import com.sdry.common.TableDataInfo;
import com.sdry.system.entity.SysUser;
import com.sdry.untils.FileUtil;
import com.sdry.untils.ResponseUtil;
import com.sdry.work.entity.Contract;
import com.sdry.work.entity.WorkPlan;
import com.sdry.work.service.WorkService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author lian
 * @date 2020-04-17 14:48
 * 办公类的模块，有工作计划，合同
 */

@RequestMapping(value = "/work")
@Controller
public class WorkController extends BaseController {

    @Resource
    private WorkService workService;

    /**
     * 进合同管理页面
     * @return 返回页面路径
     */
    @RequestMapping("/goApprovalContract")
    public String goApprovalContract(){
        return "/work/approvalContract";
    }

    /**
     * 进合同管理页面
     * @return 返回页面路径
     */
    @RequestMapping("/goContract")
    public String goContract(){
        return "/work/contract";
    }

    /**
     * 进待审批工作计划页面
     * @return 返回页面路径
     */
    @RequestMapping("/goApprovalWorkPlan")
    public String goApprovalWorkPlan(){
        return "/work/approvalWorkPlan";
    }

    /**
     * 进工作计划页面
     * @return 返回页面路径
     */
    @RequestMapping("/goWorkPlan")
    public String goWorkPlan(){
        return "/work/workPlan";
    }

    /**
     * 分页查询工作计划
     * @return 返回null,没有查到数据
     */
    @ApiOperation(value = "获取工作计划列表",notes = "分页查询")
    @ApiImplicitParam(name = "queryWorkPlan",value = "工作计划实体WorkPlan",required = true,dataType = "WorkPlan")
    @RequestMapping(value = "/queryWorkPlan")
    @ResponseBody
    public TableDataInfo queryWorkPlan(WorkPlan workPlan) {
        logger.info("分页查询班组类型 AttendanceTeamType = " + JSONObject.fromObject(workPlan));
        List<WorkPlan> list = workService.queryWorkPlan(workPlan);
        Long count = workService.countWorkPlan(workPlan);
        return getDataTable(list, count);
    }

    /**
     * 插入工作计划返回id
     * @param param 实体条件
     * @param response 给页面传值
     */
    @RequestMapping("/insertWorkPlan")
    public void insertWorkPlan(WorkPlan param, HttpServletResponse response, HttpSession session){
        logger.info("给工作计划表，插入内容："+ JSONArray.fromObject(param));
        SysUser user = (SysUser)session.getAttribute("user");
        if(user != null){
            param.setUserId(user.getUserId());
        }
        Long id = workService.insertWorkPlan(param);
        logger.info("成功返回id="+id);
        try {
            ResponseUtil.write(response,id);
        } catch (Exception e) {
            logger.info("插入失败");
            e.printStackTrace();
        }
    }

    /**
     * 根据ID修改工作计划
     * @param param 实体条件
     * @param response 给页面传值
     */
    @RequestMapping("/updateWorkPlan")
    public void upatetWorkPlan(WorkPlan param, HttpServletResponse response){
        logger.info("给工作计划表，修改内容："+ JSONArray.fromObject(param));
        Long id = workService.updateWorkPlan(param);
        logger.info("修改成功返回影响行数="+id);
        try {
            ResponseUtil.write(response,id);
        } catch (Exception e) {
            logger.info("修改失败");
            e.printStackTrace();
        }
    }

    /**
     * 根据ID删除工作计划
     * @param ids ID拼接的字符串
     * @param response 给页面传值
     */
    @RequestMapping("/deleteWorkPlan")
    public void deleteWorkPlan(String ids, HttpServletResponse response){
        logger.info("删除工作计划表，ID字符串：" + ids);
        Long id = workService.deleteWorkPlan(ids);
        logger.info("删除成功返回影响行数="+id);
        try {
            ResponseUtil.write(response,id);
        } catch (Exception e) {
            logger.info("删除失败");
            e.printStackTrace();
        }
    }

    /**
     * 分页查询合同
     * @return 返回null,没有查到数据
     */
    @ApiOperation(value = "获取合同列表",notes = "分页查询")
    @ApiImplicitParam(name = "queryContract",value = "合同实体Contract",required = true,dataType = "Contract")
    @RequestMapping(value = "/queryContract")
    @ResponseBody
    public TableDataInfo queryContract(Contract contract) {
        logger.info("分页查询合同 Contract = " + JSONObject.fromObject(contract));
        List<Contract> list = workService.queryContract(contract);
        Long count = workService.countContract(contract);
        return getDataTable(list, count);
    }

    /**
     * 插入合同返回id
     * @param param 实体条件
     * @param response 给页面传值
     */
    @RequestMapping("/insertContract")
    public void insertContract(Contract param, HttpServletResponse response, HttpSession session){
        logger.info("给合同表，插入内容："+ JSONArray.fromObject(param));
        SysUser user = (SysUser)session.getAttribute("user");
        if(user != null){
            param.setCreateId(user.getUserId());
        }
        Long id = workService.insertContract(param);
        logger.info("新增合同成功返回id="+id);
        try {
            ResponseUtil.write(response,id);
        } catch (Exception e) {
            logger.info("插入失败");
            e.printStackTrace();
        }
    }

    /**
     * 根据ID修改合同
     * @param param 实体条件
     * @param response 给页面传值
     */
    @RequestMapping("/updateContract")
    public void updatetContract(Contract param, HttpServletResponse response){
        logger.info("根据ID修改合同，修改内容："+ JSONArray.fromObject(param));
        Long id = workService.updateContract(param);
        logger.info("修改合同成功返回影响行数="+id);
        try {
            ResponseUtil.write(response,id);
        } catch (Exception e) {
            logger.info("修改失败");
            e.printStackTrace();
        }
    }

    /**
     * 根据ID删除合同
     * @param ids ID拼接的字符串
     * @param response 给页面传值
     */
    @RequestMapping("/deleteContract")
    public void deleteContract(String ids, HttpServletResponse response){
        logger.info("删除合同表，ID字符串：" + ids);
        Long id = workService.deleteContract(ids);
        logger.info("删除合同成功，返回影响行数="+id);
        try {
            ResponseUtil.write(response,id);
        } catch (Exception e) {
            logger.info("删除合同失败");
            e.printStackTrace();
        }
    }

    /**
     * 图片上传
     * @param file 文件类型
     * @param request 请求
     */
    @ResponseBody
    @RequestMapping("/upload")
    public Map upload(MultipartFile file, HttpServletRequest request){
        logger.info("图片上传的方法，图片名称：" + file);
        return workService.uploadImg(file, request);
    }

    /**
     * 文件上传
     * @param file 文件类型
     * @param request 请求
     */
    @ResponseBody
    @RequestMapping("/uploadFile")
    public Map uploadFile(MultipartFile file, HttpServletRequest request){
        logger.info("文件上传的方法，文件名称：" + file);
        return workService.uploadFile(file, request);
    }

    /**
     * 查询所有未删除的用户
     * @return 返回null，没查到数据
     */
    @ResponseBody
    @RequestMapping("/getAllUser")
    public List<SysUser> getAllUser(){
        return workService.getAllUser();
    }

    /**
     * 文件下载
     * @param path 文件路径
     * @param request 请求
     */
    @ResponseBody
    @RequestMapping("/downLoad")
    public void downLoad(String path, HttpServletRequest request, HttpServletResponse response){
        logger.info("文件下载的方法，文件路径：" + path);
        FileUtil.download2(path, response);
        logger.info("文件下载");
    }

    /**
     * 分页查询待审批工作计划
     * @return 返回null,没有查到数据
     */
    @ApiOperation(value = "获取工作计划列表",notes = "分页查询")
    @ApiImplicitParam(name = "queryApprovalWorkPlan",value = "工作计划实体WorkPlan",required = true,dataType = "WorkPlan")
    @RequestMapping(value = "/queryApprovalWorkPlan")
    @ResponseBody
    public TableDataInfo queryApprovalWorkPlan(WorkPlan workPlan) {
        logger.info("分页查询班组类型 WorkPlan = " + JSONObject.fromObject(workPlan));
        List<WorkPlan> list = workService.queryApprovalWorkPlan(workPlan);
        Long count = workService.countApprovalWorkPlan(workPlan);
        return getDataTable(list, count);
    }

    /**
     * 根据ID修改工作计划状态
     * @param param 实体条件
     * @param response 给页面传值
     */
    @RequestMapping("/updateWorkPlanStatus")
    public void updateWorkPlanStatus(WorkPlan param, HttpServletResponse response){
        logger.info("给工作计划表，修改内容："+ JSONArray.fromObject(param));
        Long id = workService.updateWorkPlanStatus(param);
        update(id, response);
    }

    /**
     * 分页查询待审批合同
     * @return 返回null,没有查到数据
     */
    @ApiOperation(value = "获取合同列表",notes = "分页查询")
    @ApiImplicitParam(name = "queryApprovalContract",value = "合同实体Contract",required = true,dataType = "Contract")
    @RequestMapping(value = "/queryApprovalContract")
    @ResponseBody
    public TableDataInfo queryApprovalContract(Contract contract) {
        logger.info("分页查询合同 参数：contract = " + JSONObject.fromObject(contract));
        List<Contract> list = workService.queryApprovalContract(contract);
        Long count = workService.countApprovalContract(contract);
        return getDataTable(list, count);
    }

    /**
     * 根据ID修改合同状态
     * @param param 实体条件
     * @param response 给页面传值
     */
    @RequestMapping("/updateContractStatus")
    public void updateContractStatus(Contract param, HttpServletResponse response){
        logger.info("给合同表，修改内容："+ JSONArray.fromObject(param));
        Long id = workService.updateContractStatus(param);
        update(id, response);
    }

    /**
     * 公用的修改类的，给页面返回值的方法
     * @param id 影响行数
     * @param response  传值
     */
    public void update(Long id, HttpServletResponse response){
        logger.info("修改成功,返回影响行数="+id);
        try {
            ResponseUtil.write(response,id);
        } catch (Exception e) {
            logger.info("修改失败");
            e.printStackTrace();
        }
    }

}
